package my.stock.configure;

import my.stock.util.BrowserBuilder;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@Configuration
public class BrowserConfig {

    @Value("${browser.driver.path}")
    private String path;
    @Value("${browser.root.path}")
    private String root;

    @Bean
    public ChromeDriver browser() throws IOException {
        path = new ClassPathResource(path).getURL().getPath();
        path = URLDecoder.decode(path, StandardCharsets.UTF_8.name());

        ChromeDriver browser = BrowserBuilder.build(path).getDriver();
        browser.get(root);
        return browser;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getRoot() {
        return root;
    }

    public void setRoot(String root) {
        this.root = root;
    }
}
